package com.xunheyun.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xunheyun.service.IServerService;
import com.xunheyun.utils.ConsoleUtils;
import com.xunheyun.vo.HostServer;

@Controller
@RequestMapping(value="/server")
public class ServerController {

	
	@Autowired
	private IServerService serverService;
	
	/**
	 * 服务器列表
	 */
	@RequestMapping(value="/list/{configServerId}")
	public String list(HttpServletRequest request, @PathVariable int configServerId){
		
		
		List<HostServer> list = serverService.list(configServerId);
		
		request.setAttribute("servers", list);
		request.setAttribute("configServerId", configServerId);
		
		return "/server/server_list";
	}
	
	/**
	 * 添加服务器配置信息
	 */
	@RequestMapping(value="/addpage/{configServerId}")
	public String addpage(HttpServletRequest request,@PathVariable int configServerId){
		
		request.setAttribute("configServerId", configServerId);
		
		return "/server/server_add";
	}
	
	@RequestMapping(value="/add")
	@ResponseBody
	public String add(@ModelAttribute("form") HostServer server){
		
		serverService.add(server);
		
		return "success";
	}
	
	/**
	 * 删除服务器配置信息
	 * @param serverId
	 * @return	success
	 */
	@RequestMapping(value="/delete/{serverId}")
	@ResponseBody
	public String delete(HttpServletRequest request,@PathVariable int serverId){
		
		serverService.delete(serverId);
		
		return "success";
	}
	
	@RequestMapping(value="/minlist/{configServerId}/{configId}")
	public String serverMinList(HttpServletRequest request,@PathVariable int configServerId,@PathVariable int configId){
		
		List<HostServer> list = serverService.list(configServerId);
		
		request.setAttribute("servers", list);
		request.setAttribute("configServerId", configServerId);
		request.setAttribute("configId", configId);
		
		return "/server/server_min_list";
	}
	// 打开 console 控制台
	@RequestMapping(value="/console")
	public String console(){
		return "console";
	}
	
	// 显示 console 的信息
	@RequestMapping(value="/consoleInfo")
	@ResponseBody
	public String consoleInfo(){
		
		return ConsoleUtils.console.toString();
	}
	
}
